package com.gs.synedocket.control;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.gs.synedocket.R;
import com.gs.synedocket.util.DensityUtil;


public class TitleView extends FrameLayout {

    private boolean isOpen = true;
    private ViewGroup viewGroup;
    private int height;

    public TitleView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public TitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TitleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TitleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        String title = ta.getString(R.styleable.TitleView_title_text);

        View view = LayoutInflater.from(context).inflate(R.layout.view_title, this, false);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(title);
        ImageView ivUp = view.findViewById(R.id.iv_up);
        addView(view);
        ta.recycle();
        this.setOnClickListener(v -> {
            if (viewGroup == null) {
                return;
            }
            if (isOpen) {
                initAnim(ivUp, R.anim.rotate_animdown);
                viewHide();
                isOpen = false;
            } else {
                initAnim(ivUp, R.anim.rotate_animup);
                viewShow();
                isOpen = true;
                viewGroup.setVisibility(VISIBLE);
            }
            if (onAnimEndListener != null) {
                onAnimEndListener.onAnimEndListener(isOpen);
            }
        });
    }

    private OnAnimEndListener onAnimEndListener;

    public void setOnAnimEndListener(OnAnimEndListener onAnimEndListener) {
        this.onAnimEndListener = onAnimEndListener;
    }

    public interface OnAnimEndListener {
        void onAnimEndListener(boolean isOpen);
    }

    private void viewShow() {
        ValueAnimator valueAnimator = createDropAnimator(viewGroup, 0, height);
        valueAnimator.setDuration(400);
        valueAnimator.start();
    }

    private void viewHide() {
        height = DensityUtil.displayViewHeight(viewGroup);
        ValueAnimator valueAnimator = createDropAnimator(viewGroup, height, 0);
        valueAnimator.setDuration(400);
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                viewGroup.setVisibility(GONE);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        valueAnimator.start();
    }

    private ValueAnimator createDropAnimator(View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(animation -> {
            int value = (int) animation.getAnimatedValue();
            ViewGroup.LayoutParams lp = v.getLayoutParams();
            lp.height = value;
            v.setLayoutParams(lp);
        });
        return animator;
    }

    public void setContent(ViewGroup view) {
        viewGroup = view;
    }

    private void initAnim(ImageView iv, int anim) {
        Animation animation = AnimationUtils.loadAnimation(getContext(), anim);
        animation.setFillAfter(true);
        iv.startAnimation(animation);
    }

}
